/**
 * Created by Administrator on 2017/12/21.
 */
/**
 * 栏目管理初始化
 */
var Category = {
    id: "CategoryTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Category.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '简介', field: 'intro', align: 'center', valign: 'middle', sortable: true},
        {title: '类型', field: 'type', align: 'center', valign: 'middle', sortable: true},
        {title: '所属栏目', field: 'bannerName', align: 'center', valign: 'middle', sortable: true},
        {title: '信息量', field:'cateCount',align:'center',valign:'middle',sortable: true},
        {title: '推荐排序值', field:'recRate',align:'center',valign:'middle',sortable: true},
        {title: '创建时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true}
    ];
};

/**
 * 检查是否选中
 */
Category.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Category.seItem = selected[0];
        return true;
    }
};

/**
 * 批量检查是否选中
 */
Category.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Category.seItem = selected;
        return true;
    }
};

/**
 * 点击添加专辑
 */
Category.openAddCategory = function () {
    var index = layer.open({
        type: 2,
        title: '添加专辑',
        area: ['800px', '480px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/category/radio/categoryAdd'
    });
    this.layerIndex = index;
};

/**
 * 打开查看专辑详情
 */
Category.openCategoryDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '专辑修改',
            area: ['800px', '480px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/category/radio/categoryUpdate/' + Category.seItem.id
        });
        this.layerIndex = index;
    }
};

Category.batchUpBanner = function () {
    if(this.batchCheck()) {
        var index = layer.open({
            type: 2,
            title: '所属栏目批量修改',
            area: ['400px', '300px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/category/radio/batchUpPage/'
        });
        this.layerIndex = index;
    }
}

/**
 * 删除栏目
 */
Category.delete = function () {
    if (this.batchCheck()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/category/radio/delete", function (data) {
                Feng.success("删除成功!");
                Category.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var ids = "";
            for (var i = 0;i < Category.seItem.length;i++) {
                if(i < Category.seItem.length - 1) {
                    ids += Category.seItem[i].id + ",";
                } else {
                    ids += Category.seItem[i].id;
                }
            }
            ajax.set("categoryIds", ids);
            ajax.start();
        };

        var names = "";
        for (var i = 0; i < Category.seItem.length;i++) {
            if(i < Category.seItem.length - 1) {
                names += Category.seItem[i].name +"  ";
            } else {
                names += Category.seItem[i].name;
            }
        }
        Feng.confirm("是否删除栏目 " + names + "?", operation);
    }
};

/**
 * 查询栏目列表
 */
Category.search = function () {
    var queryData = {};
    queryData['condition'] = $("#condition").val();
    queryData['bannerId'] = $("#bannerCondition").find("option:selected").val();
    Category.table.refresh({query: queryData});
};

/**
 * 重置搜索条件
 */
Category.resetSearch = function () {
    $("#condition").val("");
    $("#bannerCondition option").each(function () {
       $(this).removeAttr('selected');
    });
    Category.search();
};

$(function () {
    var defaultColunms = Category.initColumn();
    var table = new BSTable(Category.id, "/category/radio/list", defaultColunms);
    table.setPaginationType("client");
    Category.table = table.init();
});
